package day7;

import java.util.Arrays;
import java.util.Scanner;

import org.junit.Test;

public class StringDemo {
	public static void main(String[] args) {
		String str1 = "Hello";
		String str2 = new String("Hello");
		String str = "Java AndroidA";
		char[] array = str.toCharArray();
		// ['H', 'e', 'l', 'l', 'o']
		char ch = str.charAt(5);
		System.out.println(ch);
		System.out.println(str.length());// 13
		System.out.println(str.indexOf('A')); // 5
		// 在此对象表示的字符序列中第一次出现该字符的索引；如果未出现该字符，则返回-1
		System.out.println(str.indexOf('A', 6));// 12
		System.out.println(str.indexOf("And"));// 5
		System.out.println(str.lastIndexOf('A'));// 12
		System.out.println(str.contains("And"));// true
		System.out.println(str.startsWith("Java A"));// true
		// offset:偏移
		System.out.println(str.startsWith("And", 5));// true
		System.out.println(str.isEmpty());// flase
		System.out.println("".isEmpty());// true
		char ch1 = ' ';// 空格字符
		String s = "";// 空字符串
	}
	
	@Test
	public void test2() {
		String str1 = "Java";
		String str2 = "JAva";
		// case sensitive
		System.out.println(str1.equals(str2));// false
		System.out.println(str1.equalsIgnoreCase(str2));// true
	}
	
	
	
	@Test
	public void test3() {
		String str1 = "abc";
		String str2 = new String("abc");
		String str3 = "abc";
		String str4 = new String("abc");
		// == 是判断地址是不是相等
		System.out.println(str1 == str2);// false
		System.out.println(str1 == str3);// true
		System.out.println(str3 == str2);// false
		System.out.println(str2 == str4);// false
		// equals 是判断内容是不是相等
		System.out.println(str1.equals(str2));// true
		System.out.println(str2.equals(str3));// true
		System.out.println(str1.equals(str3));// true
	}
	
	
	@Test
	public void test4() {
		char[] array = { 'j', 'a', 'v', 'a', ' ', 'A', 'n', 'd'};//
		String str = new String(array);
		System.out.println(str);
		char[] charArray = str.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			System.out.print(charArray[i] + " ");
		}
		System.out.println();
		System.out.println(Arrays.toString(charArray));
	}
	
	@Test
	public void test5() {
		String str = "Java AndroidA";
		String newStr = str.replace('A', 'B');
		System.out.println(str);
		System.out.println(newStr);
	}
	
	@Test
	public void test6() {
		String str = "Java Android Python";
		String[] array = str.split(" ");
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i] + " ");
		}
	}
	
	@Test
	public void test7() {
		String str = " Java AndroidA ";
		System.out.println(str.substring(5));
		System.out.println(str.substring(5, 8));// endIndex - the ending index, exclusive.
		System.out.println(str.trim());// 去除前后的空格
	}
	
	@Test
	public void test8() {
		Scanner scanner = new Scanner(System.in);
		String str = scanner.nextLine();
		scanner.close();
		char[] array = str.toCharArray();
		int start = 0;
		int end = array.length - 1;
		
		while (start < end && array[start]/*str.charAt(start)*/ == ' ') {
			start++;
		}
		while (start <= end && array[end] == ' ') {
			end--;
		}
		String newStr = new String(array);
		System.out.println(newStr.substring(start, end + 1));
	}
	
	@Test
	public void test9() {
		int num1 = 3;
		int num2 = 5;
		int max = getMax(num1, num2);
		System.out.println(max);
		
		int m = Math.max(3, 5);
		System.out.println(m);
		
	}

	private int getMax(int num1, int num2) {
		// TODO Auto-generated method stub
//		if (num1 >= num2) {
//			return num1;
//		} else {
//			return num2;
//		}
		
		
		// 三目运算符
		return num1 >= num2 ? num1 : num2;
	}
	
	@Test
	public void test10() {
		String str1 = "Java";
		String str2 = "Andorid";
		String str3 = "Python";
		
		//str1 = str1 + str2 + str3;
		//System.out.println(str1);
		
		StringBuffer buffer = new StringBuffer(str1);
		buffer.append(str2);
		buffer.append(str3);
		System.out.println(buffer);
		
		StringBuilder builder = new StringBuilder();
		// 链式
		builder.append(str1).append(str2).append(str3);
	}
}
